package com.lipuwater.agent.utils.link;

/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2017/3/14.
 * Email:924686754@qq.com
 */
public class LinkCallTask {

    private NextInterface[] mNextInterfaces = null;
    private CompleteInterface mCompleteInterface = null;
    private ErrorInterface mErrorInterface = null;
    private int mCurrentNext = -1;

    /**
     * 设置执行步骤，从上到下
     *
     * @param nextInterface
     * @return
     */
    public LinkCallTask setNext(NextInterface... nextInterface) {
        this.mNextInterfaces = nextInterface;
        return this;
    }

    /**
     * 获取当前步骤
     *
     * @return
     */
    public int getNext() {
        return mCurrentNext++;
    }

    /**
     * 设置完成调用的函数
     *
     * @param complete
     * @return
     */
    public LinkCallTask setComplete(CompleteInterface complete) {
        this.mCompleteInterface = complete;
        return this;
    }

    public LinkCallTask setError(ErrorInterface error) {
        this.mErrorInterface = error;
        return this;
    }

    /**
     * 回滚，回到上一步
     */
    public void toLast() {
        if (mNextInterfaces == null) {
            return;
        }
        mCurrentNext--;
        if (mCurrentNext == mNextInterfaces.length) {
            toComplete();
        } else if (mNextInterfaces[mCurrentNext] != null) {
            mNextInterfaces[mCurrentNext].onNext(null);
        }
    }

    /**
     * 开始从0调用
     */
    public void toStart() {
        if (mNextInterfaces == null) {
            return;
        }
        mCurrentNext = -1;
        mCurrentNext++;
        if (mCurrentNext == mNextInterfaces.length) {
            toComplete();
        } else if (mNextInterfaces[mCurrentNext] != null) {
            mNextInterfaces[mCurrentNext].onNext(null);
        }
    }

    /**
     * 下一步
     */
    public void toNext() {
        if (mNextInterfaces == null) {
            return;
        }
        mCurrentNext++;
        if (mCurrentNext == mNextInterfaces.length) {
            toComplete();
        } else if (mNextInterfaces[mCurrentNext] != null) {
            mNextInterfaces[mCurrentNext].onNext(null);
        }
    }

    /**
     * 下一步
     *
     * @param object
     */
    public void toNext(Object object) {
        if (mNextInterfaces == null) {
            return;
        }
        mCurrentNext++;
        if (mCurrentNext == mNextInterfaces.length) {
            toComplete();
        } else if (mNextInterfaces[mCurrentNext] != null) {
            mNextInterfaces[mCurrentNext].onNext(object);
        }
    }

    /**
     * 重新执行当前步骤
     */
    public void toRepeat() {
        if (mNextInterfaces == null) {
            return;
        }
        if (mCurrentNext == mNextInterfaces.length) {
            toComplete();
        } else if (mNextInterfaces[mCurrentNext] != null) {
            mNextInterfaces[mCurrentNext].onNext(null);
        }
    }

    /**
     * 重新执行当前步骤
     */
    public void toRepeat(Object object) {
        if (mNextInterfaces == null) {
            return;
        }
        if (mCurrentNext == mNextInterfaces.length) {
            toComplete();
        } else if (mNextInterfaces[mCurrentNext] != null) {
            mNextInterfaces[mCurrentNext].onNext(object);
        }
    }

    /**
     * 跳转到完成
     */
    public void toComplete() {
        if (mCompleteInterface == null) {
            return;
        }
        mCurrentNext = -1;
        if (mCompleteInterface != null) {
            mCompleteInterface.onComplete(null);
        }
    }

    public void toComplete(Object object) {
        if (mCompleteInterface == null) {
            return;
        }
        mCurrentNext = -1;
        if (mCompleteInterface != null) {
            mCompleteInterface.onComplete(object);
        }
    }

    /**
     * 跳转到错误
     */
    public void toError() {
        if (mErrorInterface == null) {
            return;
        }
        mCurrentNext = -1;
        if (mErrorInterface != null) {
            mErrorInterface.onError(null);
        }
    }

    public void toError(Object object) {
        if (mErrorInterface == null) {
            return;
        }
        mCurrentNext = -1;
        if (mErrorInterface != null) {
            mErrorInterface.onError(object);
        }
    }

    public void unRegister() {
        mNextInterfaces = null;
        mCompleteInterface = null;
        mErrorInterface = null;
    }
}
